﻿using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json;
using System.Xml.Linq;
using VIA.SiMoJi.Domain.Vision;

namespace VIA.SiMoJi.Domain.Inspection
{
    public class SignalPanel
    {
        public static volatile SignalPanel Signal = Reload();
        public bool AllowGongLiaoSensor { get; set; }
        /// <summary>
        /// 供料盘到位
        /// </summary>
        public bool FeedTrayReady { get; set; }
        /// <summary>
        /// 料盘的料已经取完
        /// </summary>
        public bool FeedTrayCompleted { get; set; }
        /// <summary>
        /// 搬运到撕膜工位完成
        /// </summary>
        public bool BanYunCompleted { get; set; }
        /// <summary>
        /// 请求再次确认飞达（用来确认飞达是否就绪）不需要保存
        /// </summary>
        [JsonIgnore]
        public bool FeedrNotReady { get; set; }
        /// <summary>
        /// 飞达就绪已完成
        /// </summary>
        [JsonIgnore]
        public bool FeedrReadyDone { get; set; }
        /// <summary>
        /// 撕膜准备完成
        /// </summary>
        public bool SiMoReady { get; set; }
        public bool IsUseSiMo1 { get; set; }
        /// <summary>
        /// 撕膜动作完成
        /// </summary>
        public bool SiMoCompleted { get; set; }
        /// <summary>
        /// 当前料盘编号
        /// </summary>
        public string CurrentFeedTray { get; set; }

        public double[] JawOffsets = new double[7];
        public Pose FeedTrayOffset { get; set; } = new();

        SignalPanel()
        {
        }

        public void Reset()
        {
            FeedTrayReady = false;
            FeedTrayCompleted = false;
            BanYunCompleted = false;
            SiMoReady = false;
            SiMoCompleted = false;
        }

        public void Save()
        {
            try
            {
                var str = JsonConvert.SerializeObject(this);
                string path = Path.Combine(Environment.CurrentDirectory, "RunningRecord", "Signal.json");
                if (!File.Exists(path))
                {
                    var stream = File.Create(path);
                    stream.Dispose();
                }
                using StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(str);

            }
            catch (Exception ex)
            {

                throw;
            }

        }
        static SignalPanel Reload()
        {
            try
            {
                string path = Path.Combine(Environment.CurrentDirectory, "RunningRecord", "Signal.json");
                path = Path.ChangeExtension(path, ".json");
                if (!File.Exists(path))
                {
                    var file = File.Create(path);
                    file.Dispose();
                }
                using StreamReader sw = new StreamReader(path);
                var str = sw.ReadToEnd();
                SignalPanel record = JsonConvert.DeserializeObject<SignalPanel>(str);
                record ??= new SignalPanel() { };
                return record;

            }
            catch (Exception)
            {
                return new SignalPanel();
            }
        }
    }
}
